/*
 * Copyright (C) 2008,2009,2010,2011  Paulo Cesar Pereira de Andrade.
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Authors:
 *	Paulo Cesar Pereira de Andrade
 */

#include "exl.h"

#define r0			thread_self->r0
#define ss			thread_self->ess
#define bp			thread_self->ebp
#define sp			thread_self->esp
#define obj			thread_self->obj
#define ret			thread_self->ret
#define flt			thread_self->flt

static void
mul2exp(evalue_t *v, eint32_t shift);

static void
div2exp(evalue_t *v, eint32_t shift);

static void
arith_shl(evalue_t *v, eint32_t shift);

static void
arith_shr(evalue_t *v, eint32_t shift);

/*
 * Implementation
 */
void
evm_i_ne(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.v.i = 0 != r0.v.i;
	    break;
	case t_int:
	    r0.v.i = v->v.i != r0.v.i;
	    break;
	case t_float:
	    r0.v.i = v->v.d != r0.v.i;
	    break;
	case t_mpz:		case t_shz:
	    r0.v.i = empz_cmp_i(v->v.mpz, r0.v.i) != 0;
	    break;
	case t_mpr:		case t_shr:
	    empr_set_i(thr_rr0, r0.v.i);
	    r0.v.i = !mpfr_equal_p(v->v.mpr, thr_rr0);
	    break;
	default:
	    r0.v.i = 1;
	    break;
    }
}

void
evm_i_lt(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.v.i = 0 < r0.v.i;
	    break;
	case t_int:
	    r0.v.i = v->v.i < r0.v.i;
	    break;
	case t_float:
	    r0.v.i = v->v.d < r0.v.i;
	    break;
	case t_mpz:		case t_shz:
	    r0.v.i = empz_cmp_i(v->v.mpz, r0.v.i) < 0;
	    break;
	case t_mpq:		case t_shq:
	    r0.v.i = empq_cmp_i(v->v.mpq, r0.v.i) < 0;
	    break;
	case t_mpr:		case t_shr:
	    empr_set_i(thr_rr0, r0.v.i);
	    r0.v.i = mpfr_less_p(v->v.mpr, thr_rr0);
	    break;
	default:
	    evm_raise(except_not_a_real_number);
    }
}

void
evm_i_le(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.v.i = 0 <= r0.v.i;
	    break;
	case t_int:
	    r0.v.i = v->v.i <= r0.v.i;
	    break;
	case t_float:
	    r0.v.i = v->v.d <= r0.v.i;
	    break;
	case t_mpz:		case t_shz:
	    r0.v.i = empz_cmp_i(v->v.mpz, r0.v.i) <= 0;
	    break;
	case t_mpq:		case t_shq:
	    r0.v.i = empq_cmp_i(v->v.mpq, r0.v.i) <= 0;
	    break;
	case t_mpr:		case t_shr:
	    empr_set_i(thr_rr0, r0.v.i);
	    r0.v.i = mpfr_lessequal_p(v->v.mpr, thr_rr0);
	    break;
	default:
	    evm_raise(except_not_a_real_number);
    }
}

void
evm_i_eq(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.v.i = 0 == r0.v.i;
	    break;
	case t_int:
	    r0.v.i = v->v.i == r0.v.i;
	    break;
	case t_float:
	    r0.v.i = v->v.d == r0.v.i;
	    break;
	case t_mpz:		case t_shz:
	    r0.v.i = empz_cmp_i(v->v.mpz, r0.v.i) == 0;
	    break;
	case t_mpr:		case t_shr:
	    empr_set_i(thr_rr0, r0.v.i);
	    r0.v.i = mpfr_equal_p(v->v.mpr, thr_rr0);
	    break;
	default:
	    r0.v.i = 0;
	    break;
    }
}

void
evm_i_ge(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.v.i = 0 >= r0.v.i;
	    break;
	case t_int:
	    r0.v.i = v->v.i >= r0.v.i;
	    break;
	case t_float:
	    r0.v.i = v->v.d >= r0.v.i;
	    break;
	case t_mpz:		case t_shz:
	    r0.v.i = empz_cmp_i(v->v.mpz, r0.v.i) >= 0;
	    break;
	case t_mpq:		case t_shq:
	    r0.v.i = empq_cmp_i(v->v.mpq, r0.v.i) >= 0;
	    break;
	case t_mpr:		case t_shr:
	    empr_set_i(thr_rr0, r0.v.i);
	    r0.v.i = mpfr_greaterequal_p(v->v.mpr, thr_rr0);
	    break;
	default:
	    evm_raise(except_not_a_real_number);
    }
}

void
evm_i_gt(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.v.i = 0 > r0.v.i;
	    break;
	case t_int:
	    r0.v.i = v->v.i > r0.v.i;
	    break;
	case t_float:
	    r0.v.i = v->v.d > r0.v.i;
	    break;
	case t_mpz:		case t_shz:
	    r0.v.i = empz_cmp_i(v->v.mpz, r0.v.i) > 0;
	    break;
	case t_mpq:		case t_shq:
	    r0.v.i = empq_cmp_i(v->v.mpq, r0.v.i) > 0;
	    break;
	case t_mpr:		case t_shr:
	    empr_set_i(thr_rr0, r0.v.i);
	    r0.v.i = mpfr_greater_p(v->v.mpr, thr_rr0);
	    break;
	default:
	    evm_raise(except_not_a_real_number);
    }
}

void
evm_i_and(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.v.i = 0;
	    break;
	case t_int:
	    r0.v.i = v->v.i & r0.v.i;
	    break;
	case t_mpz:		case t_shz:
	    if (likely(ez_and_z_i(thr_zr, v->v.mpz, r0.v.i)))
		r0.v.i = empz_get_i(thr_zr);
	    else
		set_rz();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_an_integer);
	    evector_and_int(v->v.o, r0.v.i, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_i_or(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    break;
	case t_int:
	    r0.v.i = v->v.i | r0.v.i;
	    break;
	case t_mpz:		case t_shz:
	    ez_or_z_i(thr_zr, v->v.mpz, r0.v.i);
	    set_rz();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_an_integer);
	    evector_or_int(v->v.o, r0.v.i, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_i_xor(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    break;
	case t_int:
	    r0.v.i = v->v.i ^ r0.v.i;
	    break;
	case t_shz:	case t_mpz:
	    if (ez_xor_z_i(thr_zr, v->v.mpz, r0.v.i))
		r0.v.i = empz_get_i(thr_zr);
	    else
		set_rz();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_an_integer);
	    evector_xor_int(v->v.o, r0.v.i, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

static void
mul2exp(evalue_t *v, eint32_t shift)
{
    switch (v->t) {
	case t_void:
	    r0.t = t_int;
	    r0.v.i = 0;
	    break;
	case t_int:
	    if ((r0.v.i = ez_mul2_i_i(thr_zr, v->v.i, shift)) != MININT)
		r0.t = t_int;
	    else
		set_rz();
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = ef_mul2_d_i(v->v.d, shift);
	    break;
	case t_mpz:		case t_shz:
	    mpz_mul_2exp(thr_zr, v->v.mpz, shift);
	    chk_rz();
	    break;
	case t_mpq:		case t_shq:
	    switch (eq_mul2_q_i(thr_qr, v->v.mpq, shift)) {
		case 0:
		    set_rq();
		    break;
		case 1:
		    r0.t = t_int;
		    r0.v.i = empz_get_i(thr_zr);
		    break;
		default:
		    set_rz();
		    break;
	    }
	    break;
	case t_mpr:		case t_shr:
	    er_mul2_r_i(thr_rr, v->v.mpr, shift);
	    chk_rr();
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = edd_mul2_dd_i(v->v.cdd, shift);
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    eqq_mul2_qq_i(thr_qq, v->v.cqq, shift);
	    chk_rqq();
	    break;
	case t_mpc:		case t_shc:
	    ecc_mul2_cc_i(thr_c, v->v.mpc, shift);
	    chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_mul2_int32(v->v.o, shift, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

static void
div2exp(evalue_t *v, eint32_t shift)
{
    switch (v->t) {
	case t_void:
	    r0.t = t_int;
	    r0.v.i = 0;
	    break;
	case t_int:
	    if (shift) {
		if ((r0.v.i = eq_div2_i_i(thr_qr, v->v.i, shift)) != MININT)
		    r0.t = t_int;
		else
		    set_rq();
	    }
	    else {
		r0.t = t_int;
		r0.v.i = v->v.i;
	    }
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = ef_div2_d_i(v->v.d, shift);
	    break;
	case t_mpz:		case t_shz:
	    switch (eq_div2_z_i(thr_qr, v->v.mpz, shift)) {
		case 0:
		    set_rq();
		    break;
		case 1:
		    r0.t = t_int;
		    r0.v.i = empz_get_i(thr_zr);
		    break;
		default:
		    set_rz();
		    break;
	    }
	    break;
	case t_mpq:		case t_shq:
	    switch (eq_div2_q_i(thr_qr, v->v.mpq, shift)) {
		case 0:
		    set_rq();
		    break;
		case 1:
		    r0.t = t_int;
		    r0.v.i = empz_get_i(thr_zr);
		    break;
		default:
		    set_rz();
		    break;
	    }
	    break;
	case t_mpr:		case t_shr:
	    er_div2_r_i(thr_rr, v->v.mpr, shift);
	    set_rr();
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = edd_div2_dd_i(v->v.cdd, shift);
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    eqq_div2_qq_i(thr_qq, v->v.cqq, shift);
	    chk_rqq();
	    break;
	case t_mpc:		case t_shc:
	    ecc_div2_cc_i(thr_c, v->v.mpc, shift);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_div2_int32(v->v.o, shift, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_i_mul2(evalue_t *v, eint32_t shift)
{
    if (likely(shift >= 0))
	mul2exp(v, shift);
    else
	div2exp(v, -shift);
}

void
evm_i_div2(evalue_t *v, eint32_t shift)
{
    if (likely(shift >= 0))
	div2exp(v, shift);
    else
	mul2exp(v, -shift);
}

static void
arith_shl(evalue_t *v, eint32_t shift)
{
    switch (v->t) {
	case t_void:
	    r0.t = t_int;
	    r0.v.i = 0;
	    break;
	case t_int:
	    r0.t = t_int;
	    if ((r0.v.i = ez_shl_i_i(thr_zr, v->v.i, shift)) == MININT)
		set_rz();
	    break;
	case t_float:
	    if (ez_shl_d_i(thr_zr, v->v.d, shift)) {
		r0.t = t_int;
		r0.v.i = empz_get_i(thr_zr);
	    }
	    else
		chk_rz();
	    break;
	case t_mpz:		case t_shz:
	    ez_shl_z_i(thr_zr, v->v.mpz, shift);
	    chk_rz();
	    break;
	case t_mpq:		case t_shq:
	    if (ez_shl_q_i(thr_zr, v->v.mpq, shift)) {
		r0.t = t_int;
		r0.v.i = empz_get_i(thr_zr);
	    }
	    else
		set_rz();
	    break;
	case t_mpr:		case t_shr:
	    if (ez_shl_r_i(thr_zr, v->v.mpr, shift)) {
		r0.t = t_int;
		r0.v.i = empz_get_i(thr_zr);
	    }
	    else
		set_rz();
	    break;
	case t_cdd:
	case t_cqq:		case t_sqq:
	case t_mpc:		case t_shc:
		evm_raise(except_not_a_real_number);
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_real_number);
	    evector_shl_int32(v->v.o, shift, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

static void
arith_shr(evalue_t *v, eint32_t shift)
{
    switch (v->t) {
	case t_void:
	    r0.t = t_int;
	    r0.v.i = 0;
	    break;
	case t_int:
	    r0.t = t_int;
	    if (likely(shift < 64))
		r0.v.i = v->v.i >> shift;
	    else
		r0.v.i = 0;
	    break;
	case t_float:
	    r0.t = t_int;
	    if ((r0.v.i = ez_shr_d_i(thr_zr, v->v.d, shift)) == MININT)
		set_rz();
	    break;
	case t_mpz:		case t_shz:
	    if (ez_shr_z_i(thr_zr, v->v.mpz, shift)) {
		r0.t = t_int;
		r0.v.i = empz_get_i(thr_zr);
	    }
	    else
		set_rz();
	    break;
	case t_mpq:		case t_shq:
	    if (ez_shr_q_i(thr_zr, v->v.mpq, shift)) {
		r0.t = t_int;
		r0.v.i = empz_get_i(thr_zr);
	    }
	    else
		set_rz();
	    break;
	case t_mpr:		case t_shr:
	    if (ez_shr_r_i(thr_zr, v->v.mpr, shift)) {
		r0.t = t_int;
		r0.v.i = empz_get_i(thr_zr);
	    }
	    else
		set_rz();
	    break;
	case t_cdd:
	case t_cqq:		case t_sqq:
	case t_mpc:		case t_shc:
		evm_raise(except_not_a_real_number);
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_real_number);
	    evector_shr_int32(v->v.o, shift, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_i_shl(evalue_t *v, eint32_t shift)
{
    if (likely(shift >= 0))
	arith_shl(v, shift);
    else
	arith_shr(v, -shift);
}

void
evm_i_shr(evalue_t *v, eint32_t shift)
{
    if (likely(shift >= 0))
	arith_shr(v, shift);
    else
	arith_shl(v, -shift);
}

void
evm_i_add(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    break;
	case t_int:
	    {
		const eint_t i = v->v.i + r0.v.i;
		if (likely(!eadd_over_p(i, v->v.i, r0.v.i)))
		    r0.v.i = i;
		else {
		    ez_add_i_i(thr_zr, v->v.i, r0.v.i);
		    set_rz();
		}
	    }
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = v->v.d + r0.v.i;
	    break;
	case t_mpz:		case t_shz:
	    if ((r0.v.i = ez_add_z_i(thr_zr, v->v.mpz, r0.v.i)) == MININT)
		set_rz();
	    break;
	case t_mpq:		case t_shq:
	    switch (eq_add_q_i(thr_qr, v->v.mpq, r0.v.i)) {
		case 0:
		    set_rq();
		    break;
		case -1:
		    set_rz();
		    break;
		default:
		    r0.v.i = empz_get_i(thr_zr);
		    break;
	    }
	    break;
	case t_mpr:		case t_shr:
	    er_add_r_i(thr_rr, v->v.mpr, r0.v.i);
	    set_rr();
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = v->v.cdd + r0.v.i;
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    eqq_add_qq_i(thr_qq, v->v.cqq, r0.v.i);
	    set_rqq();
	    break;
	case t_mpc:		case t_shc:
	    ecc_add_cc_i(thr_c, v->v.mpc, r0.v.i);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_add_int(v->v.o, r0.v.i, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_i_sub(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    if (likely(r0.v.i != MININT))
		r0.v.i = -r0.v.i;
	    else {
		empz_set_u(thr_zr, MININT);
		set_rz();
	    }
	    break;
	case t_int:
	    {
		const eint_t i = v->v.i - r0.v.i;
		if (likely(!esub_over_p(i, v->v.i, r0.v.i)))
		    r0.v.i = i;
		else {
		    ez_sub_i_i(thr_zr, v->v.i, r0.v.i);
		    set_rz();
		}
	    }
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = v->v.d - r0.v.i;
	    break;
	case t_mpz:		case t_shz:
	    if ((r0.v.i = ez_sub_z_i(thr_zr, v->v.mpz, r0.v.i)) == MININT)
		set_rz();
	    break;
	case t_mpq:		case t_shq:
	    switch (eq_sub_q_i(thr_qr, v->v.mpq, r0.v.i)) {
		case 0:
		    set_rq();
		    break;
		case -1:
		    set_rz();
		    break;
		default:
		    r0.v.i = empz_get_i(thr_zr);
		    break;
	    }
	    break;
	case t_mpr:		case t_shr:
	    er_sub_r_i(thr_rr, v->v.mpr, r0.v.i);
	    set_rr();
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = v->v.cdd - r0.v.i;
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    eqq_sub_qq_i(thr_qq, v->v.cqq, r0.v.i);
	    set_rqq();
	    break;
	case t_mpc:		case t_shc:
	    ecc_sub_cc_i(thr_c, v->v.mpc, r0.v.i);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_sub_int(v->v.o, r0.v.i, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_i_mul(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.t = t_int;
	    r0.v.i = 0;
	    break;
	case t_int:
	    {
		const eint_t i = ei_mul_i_i(v->v.i, r0.v.i);
		if (likely(i != MININT))
		    r0.v.i = i;
		else {
		    ez_mul_i_i(thr_zr, v->v.i, r0.v.i);
		    set_rz();
		}
	    }
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = v->v.d * r0.v.i;
	    break;
	case t_mpz:		case t_shz:
	    if ((r0.v.i = ez_mul_z_i(thr_zr, v->v.mpz, r0.v.i)) == MININT)
		set_rz();
	    break;
	case t_mpq:		case t_shq:
	    switch (eq_mul_q_i(thr_qr, v->v.mpq, r0.v.i)) {
		case 0:
		    set_rq();
		    break;
		case -1:
		    set_rz();
		    break;
		default:
		    r0.v.i = empz_get_i(thr_zr);
		    break;
	    }
	    break;
	case t_mpr:		case t_shr:
	    er_mul_r_i(thr_rr, v->v.mpr, r0.v.i);
	    set_rr();
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = v->v.cdd * r0.v.i;
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    if (likely(r0.v.i != 0)) {
		eqq_mul_qq_i(thr_qq, v->v.cqq, r0.v.i);
		set_rqq();
	    }
	    break;
	case t_mpc:		case t_shc:
	    ecc_mul_cc_i(thr_c, v->v.mpc, r0.v.i);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_mul_int(v->v.o, r0.v.i, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_i_div(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    if (unlikely(r0.v.i == 0))
		evm_raise(except_floating_point_error);
	    r0.v.i = 0;
	    break;
	case t_int:
	    if (likely(v->v.i % r0.v.i)) {
		eq_div_i_i(thr_qr, v->v.i, r0.v.i);
		set_rq();
	    }
	    else
		r0.v.i = v->v.i / r0.v.i;
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = v->v.d / r0.v.i;
	    break;
	case t_mpz:		case t_shz:
	    switch (eq_div_z_i(thr_qr, v->v.mpz, r0.v.i)) {
		case 0:
		    set_rq();
		    break;
		case -1:
		    set_rz();
		    break;
		default:
		    r0.v.i = empz_get_i(thr_zr);
		    break;
	    }
	    break;
	case t_mpq:		case t_shq:
	    switch (eq_div_q_i(thr_qr, v->v.mpq, r0.v.i)) {
		case 0:
		    set_rq();
		    break;
		case -1:
		    set_rz();
		    break;
		default:
		    r0.v.i = empz_get_i(thr_zr);
		    break;
	    }
	    break;
	case t_mpr:		case t_shr:
	    er_div_r_i(thr_rr, v->v.mpr, r0.v.i);
	    set_rr();
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = v->v.cdd / r0.v.i;
	    chk_rdd();
	    break;
	case t_sqq:	case t_cqq:
	    eqq_div_qq_i(thr_qq, v->v.cqq, r0.v.i);
	    set_rqq();
	    break;
	case t_shc:	case t_mpc:
	    ecc_div_cc_i(thr_c, v->v.mpc, r0.v.i);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_div_int(v->v.o, r0.v.i, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_i_trunc2(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    /* gcc -O3 doesn't cause an exception for "0/r0.v.i" */
	    if (unlikely(r0.v.i == 0))
		evm_raise(except_floating_point_error);
	    r0.v.i = 0;
	    break;
	case t_int:
	    r0.v.i = v->v.i / r0.v.i;
	    break;
	case t_float:
	    if ((r0.v.i = ez_trunc_d(thr_zr, v->v.d / r0.v.i)) == MININT)
		set_rz();
	    break;
	case t_mpz:		case t_shz:
	    if ((r0.v.i = ez_trunc_z_i(thr_zr, v->v.mpz, r0.v.i)) == MININT)
		set_rz();
	    break;
	case t_mpq:		case t_shq:
	    if ((r0.v.i = ez_trunc_q_i(thr_zr, v->v.mpq, r0.v.i)) == MININT)
		set_rz();
	    break;
	case t_mpr:		case t_shr:
	    if ((r0.v.i = ez_trunc_r_i(thr_zr, v->v.mpr, r0.v.i)) == MININT)
		set_rz();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_real_number);
	    evector_trunc_int(v->v.o, r0.v.i, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_i_rem(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    if (unlikely(r0.v.i == 0))
		evm_raise(except_floating_point_error);
	    r0.v.i = 0;
	    break;
	case t_int:
	    r0.v.i = v->v.i % r0.v.i;
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = fmod(v->v.d, r0.v.i);
	    break;
	case t_mpz:		case t_shz:
	    r0.v.i = ez_rem_z_i(thr_zr, v->v.mpz, r0.v.i);
	    if (unlikely(r0.v.i == MININT))
		/* FIXME can this happen? (only if r0.v.i is already MININT?) */
		set_rz();
	    break;
	case t_mpq:		case t_shq:
	    switch (eq_rem_q_i(thr_qr, v->v.mpq, r0.v.i)) {
		case 0:
		    set_rq();
		    break;
		case -1:
		    set_rz();
		    break;
		default:
		    r0.v.i = empz_get_i(thr_zr);
		    break;
	    }
	    break;
	case t_mpr:		case t_shr:
	    er_rem_r_i(thr_rr, v->v.mpr, r0.v.i);
	    set_rr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_real_number);
	    evector_rem_int(v->v.o, r0.v.i, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_i_atan2(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_atan2_d_i(0.0, r0.v.i);
	    }
	    else {
		er_atan2_i_i(thr_rr, 0, r0.v.i);
		set_rr();
	    }
	    break;
	case t_int:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_atan2_i_i(v->v.i, r0.v.i);
	    }
	    else {
		er_atan2_i_i(thr_rr, v->v.i, r0.v.i);
		set_rr();
	    }
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = ed_atan2_d_i(v->v.d, r0.v.i);
	    break;
	case t_mpz:		case t_shz:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_atan2_z_i(v->v.mpz, r0.v.i);
	    }
	    else {
		er_atan2_z_i(thr_rr, v->v.mpz, r0.v.i);
		set_rr();
	    }
	    break;
	case t_mpq:		case t_shq:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_atan2_q_i(v->v.mpq, r0.v.i);
	    }
	    else {
		er_atan2_q_i(thr_rr, v->v.mpq, r0.v.i);
		set_rr();
	    }
	    break;
	case t_mpr:		case t_shr:
	    er_atan2_r_i(thr_rr, v->v.mpr, r0.v.i);
	    set_rr();
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = edd_atan2_dd_i(v->v.cdd, r0.v.i);
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    if (!flt) {
		r0.t = t_cdd;
		r0.v.cdd = edd_atan2_qq_i(v->v.cqq, r0.v.i);
		chk_rdd();
	    }
	    else {
		ecc_atan2_qq_i(thr_c, v->v.cqq, r0.v.i);
		set_chk_rrr();
	    }
	    break;
	case t_mpc:		case t_shc:
	    ecc_atan2_cc_i(thr_c, v->v.mpc, r0.v.i);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_atan2_int(v->v.o, r0.v.i, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_i_pow(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_pow_d_i(0.0, r0.v.i);
	    }
	    else {
		er_pow_i_i(thr_rr, 0, r0.v.i);
		set_rr();
	    }
	    break;
	case t_int:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_pow_i_i(v->v.i, r0.v.i);
	    }
	    else {
		er_pow_i_i(thr_rr, v->v.i, r0.v.i);
		set_rr();
	    }
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = ed_pow_d_i(v->v.d, r0.v.i);
	    break;
	case t_mpz:		case t_shz:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_pow_z_i(v->v.mpz, r0.v.i);
	    }
	    else {
		er_pow_z_i(thr_rr, v->v.mpz, r0.v.i);
		set_rr();
	    }
	    break;
	case t_mpq:		case t_shq:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_pow_q_i(v->v.mpq, r0.v.i);
	    }
	    else {
		er_pow_q_i(thr_rr, v->v.mpq, r0.v.i);
		set_rr();
	    }
	    break;
	case t_mpr:		case t_shr:
	    er_pow_r_i(thr_rr, v->v.mpr, r0.v.i);
	    set_rr();
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = edd_pow_dd_i(v->v.cdd, r0.v.i);
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    if (!flt) {
		r0.t = t_cdd;
		r0.v.cdd = edd_pow_qq_i(v->v.cqq, r0.v.i);
		chk_rdd();
	    }
	    else {
		ecc_pow_qq_i(thr_c, v->v.cqq, r0.v.i);
		set_chk_rrr();
	    }
	    break;
	case t_mpc:		case t_shc:
	    ecc_pow_cc_i(thr_c, v->v.mpc, r0.v.i);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_pow_int(v->v.o, r0.v.i, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_i_hypot(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_abs_i(r0.v.i);
	    }
	    else {
		er_abs_i(thr_rr, r0.v.i);
		set_rr();
	    }
	    break;
	case t_int:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_hypot_i_i(v->v.i, r0.v.i);
	    }
	    else {
		er_hypot_i_i(thr_rr, v->v.i, r0.v.i);
		set_rr();
	    }
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = ed_hypot_d_i(v->v.d, r0.v.i);
	    break;
	case t_mpz:		case t_shz:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_hypot_z_i(v->v.mpz, r0.v.i);
	    }
	    else {
		er_hypot_z_i(thr_rr, v->v.mpz, r0.v.i);
		set_rr();
	    }
	    break;
	case t_mpq:		case t_shq:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_hypot_q_i(v->v.mpq, r0.v.i);
	    }
	    else {
		er_hypot_q_i(thr_rr, v->v.mpq, r0.v.i);
		set_rr();
	    }
	    break;
	case t_mpr:		case t_shr:
	    er_hypot_r_i(thr_rr, v->v.mpr, r0.v.i);
	    set_rr();
	    break;
	case t_cdd:
	    r0.t = t_float;
	    r0.v.d = ed_hypot_dd_i(v->v.cdd, r0.v.i);
	    break;
	case t_cqq:		case t_sqq:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_hypot_qq_i(v->v.cqq, r0.v.i);
	    }
	    else {
		er_hypot_qq_i(thr_rr, v->v.cqq, r0.v.i);
		set_rr();
	    }
	    break;
	case t_mpc:		case t_shc:
	    er_hypot_cc_i(thr_rr, v->v.mpc, r0.v.i);
	    set_rr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_hypot_int(v->v.o, r0.v.i, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_i_complex(evalue_t *v)
{
    if (likely(r0.v.i)) {
	switch (v->t) {
	    case t_void:
		empq_set_i(thr_qi, r0.v.i);
		mpq_set_ui(thr_qr, 0, 1);
		set_rqq();
		break;
	    case t_int:
		empq_set_i(thr_qi, r0.v.i);
		empq_set_i(thr_qr, v->v.i);
		set_rqq();
		break;
	    case t_float:
		r0.t = t_cdd;
		imag(r0.v.cdd) = r0.v.i;
		real(r0.v.cdd) = v->v.d;
		chk_rdd();
		break;
	    case t_mpz:		case t_shz:
		empq_set_i(thr_qi, r0.v.i);
		mpq_set_z(thr_qr, v->v.mpz);
		set_rqq();
		break;
	    case t_mpq:		case t_shq:
		empq_set_i(thr_qi, r0.v.i);
		mpq_set(thr_qr, v->v.mpq);
		set_rqq();
		break;
	    case t_mpr:		case t_shr:
		empr_set_i(thr_ri, r0.v.i);
		mpfr_set(thr_rr, v->v.mpr, thr_rnd);
		set_chk_rrr();
		break;
	    default:
		evm_raise(except_not_a_real_number);
	}
    }
    else
	evm_v_complex(v);
}

void
evm_i_inc(void)
{
    if (likely(r0.v.i < 0x7fffffffffffffffLL))
	++r0.v.i;
    else {
	empz_set_i(thr_zr, r0.v.i);
	mpz_add_ui(thr_zr, thr_zr, 1);
	set_rz();
    }
}

void
evm_i_dec(void)
{
    if (likely(r0.v.i > -0x7fffffffffffffffLL))
	--r0.v.i;
    else {
	empz_set_i(thr_zr, r0.v.i);
	mpz_sub_ui(thr_zr, thr_zr, 1);
	set_rz();
    }
}
